//
//  XMGiftView.m
//  XunMi
//
//  Created by MacBook on 2024/4/12.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMGiftView.h"
#import "XMGiftModel.h"
#import "XMGiftCollectionViewCell.h"
#import "XMBagCollectionViewCell.h"

#import "XMGiftOpenMemberView.h"
#import "XMGiftButton.h"
#import "XMBagModel.h"

#define kSubViewH 382 - 78

@interface XMGiftView () <UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet XMGiftButton *giftButton;
@property (weak, nonatomic) IBOutlet XMGiftButton *memberButton;
@property (weak, nonatomic) IBOutlet XMGiftButton *bagButton;
@property (weak, nonatomic) IBOutlet UIButton *moneyButton;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (strong, nonatomic) UICollectionView *giftCollectionV;
@property (strong, nonatomic) UICollectionView *memberCollectionV;
@property (strong, nonatomic) UICollectionView *bagCollectionV;
@property (nonatomic, strong) XMGiftOpenMemberView *openMemberView;

@end

@implementation XMGiftView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupSubviews];
}

- (void)setGiftArray:(NSArray<XMGiftModel *> *)giftArray {
    _giftArray = giftArray;
    [giftArray firstObject].selected = YES;
    [self.giftCollectionV reloadData];
}

- (void)setMemberArray:(NSArray<XMGiftModel *> *)memberArray {
    _memberArray = memberArray;
    [memberArray firstObject].selected = YES;
    [self.memberCollectionV reloadData];
}

- (void)setBagArray:(NSArray<XMBagModel *> *)bagArray {
    _bagArray = bagArray;
    [self.bagCollectionV reloadData];
}

- (void)setIsInvite:(BOOL)isInvite {
    _isInvite = isInvite;
    self.memberCollectionV.hidden = isInvite;
    self.bagCollectionV.hidden = isInvite;
    self.memberButton.hidden = isInvite;
    self.bagButton.hidden = isInvite;
    [self.giftCollectionV reloadData];
}

- (void)resetViews {
    [self clickAct:self.giftButton];
    [self.giftArray enumerateObjectsUsingBlock:^(XMGiftModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            obj.selected = YES;
        } else {
            obj.selected = NO;
        }
    }];
    [self.giftCollectionV reloadData];
    
    if ([XMUtil isBlankString:kUserModel.member_time]) {
        self.openMemberView.hidden = NO;
        self.memberCollectionV.hidden = YES;
    } else {
        self.openMemberView.hidden = YES;
        self.memberCollectionV.hidden = NO;
        [self.memberArray enumerateObjectsUsingBlock:^(XMGiftModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == 0) {
                obj.selected = YES;
            } else {
                obj.selected = NO;
            }
        }];
        [self.memberCollectionV reloadData];
    }
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    [self radiusWithRadius:20 corner:(UIRectCornerTopLeft | UIRectCornerTopRight)];
    
    [self.giftButton setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor] forState:UIControlStateNormal];
    [self.memberButton setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor] forState:UIControlStateNormal];
    [self.bagButton setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor] forState:UIControlStateNormal];

    [self.giftButton setBackgroundImage:[UIImage imageWithColor:kAppMainColor] forState:UIControlStateSelected];
    [self.memberButton setBackgroundImage:[UIImage imageWithColor:kAppMainColor] forState:UIControlStateSelected];
    [self.bagButton setBackgroundImage:[UIImage imageWithColor:kAppMainColor] forState:UIControlStateSelected];
    
    UICollectionViewFlowLayout *giftLayout = [[UICollectionViewFlowLayout alloc] init];
    giftLayout.itemSize = CGSizeMake((kScreenWidth - 32) / 4, 116);
    giftLayout.minimumLineSpacing = 0;
    giftLayout.minimumInteritemSpacing = 0;
    
    self.giftCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kSubViewH) collectionViewLayout:giftLayout];
    self.giftCollectionV.contentInset = UIEdgeInsetsMake(0, 16, 0, 16);
    self.giftCollectionV.delegate = self;
    self.giftCollectionV.dataSource = self;
    [self.giftCollectionV registerNib:kNibWithName(kStringFromClass(XMGiftCollectionViewCell)) forCellWithReuseIdentifier:kStringFromClass(XMGiftCollectionViewCell)];
    
    
    UICollectionViewFlowLayout *memberLayout = [[UICollectionViewFlowLayout alloc] init];
    memberLayout.itemSize = CGSizeMake((kScreenWidth - 32) / 4, 116);
    memberLayout.minimumLineSpacing = 0;
    memberLayout.minimumInteritemSpacing = 0;
    
    self.memberCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kSubViewH) collectionViewLayout:memberLayout];
    self.memberCollectionV.contentInset = UIEdgeInsetsMake(0, 16, 0, 16);
    self.memberCollectionV.delegate = self;
    self.memberCollectionV.dataSource = self;
    [self.memberCollectionV registerNib:kNibWithName(kStringFromClass(XMGiftCollectionViewCell)) forCellWithReuseIdentifier:kStringFromClass(XMGiftCollectionViewCell)];
    
    
    UICollectionViewFlowLayout *bagLayout = [[UICollectionViewFlowLayout alloc] init];
    bagLayout.itemSize = CGSizeMake((kScreenWidth - 32) / 4, 116);
    bagLayout.minimumLineSpacing = 0;
    bagLayout.minimumInteritemSpacing = 0;
    
    self.bagCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(kScreenWidth * 2, 0, kScreenWidth, kSubViewH) collectionViewLayout:bagLayout];
    self.bagCollectionV.contentInset = UIEdgeInsetsMake(0, 16, 0, 16);
    self.bagCollectionV.delegate = self;
    self.bagCollectionV.dataSource = self;
    [self.bagCollectionV registerNib:kNibWithName(kStringFromClass(XMBagCollectionViewCell)) forCellWithReuseIdentifier:kStringFromClass(XMBagCollectionViewCell)];
    
    
    self.scrollerView.contentSize = CGSizeMake(kScreenWidth * 3, 0);
    self.scrollerView.showsHorizontalScrollIndicator = NO;
    self.scrollerView.delegate = self;
    self.scrollerView.pagingEnabled = YES;
    [self.scrollerView addSubview:self.giftCollectionV];
    [self.scrollerView addSubview:self.memberCollectionV];
    [self.scrollerView addSubview:self.bagCollectionV];

    
    self.openMemberView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kSubViewH);
    [self.scrollerView addSubview:self.openMemberView];
    [self.openMemberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.memberCollectionV);
    }];
    
    XMWeakSelf(self);
    self.openMemberView.memberBlock = ^{
        XMStrongSelf(self);
        if (self.memberBlock) {
            self.memberBlock();
        }
    };
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.moneyButton setImagePosition:XMImagePositionRight spacing:5];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.giftCollectionV) {
        return self.giftArray.count;
    }
    
    if (collectionView == self.memberCollectionV) {
        return self.memberArray.count;
    }
    
    if (collectionView == self.bagCollectionV) {
        return self.bagArray.count;
    }
    return 0;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.giftCollectionV) {
        XMGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMGiftCollectionViewCell) forIndexPath:indexPath];
        cell.model = [self.giftArray safe_objectAtIndex:indexPath.item];
        [cell.sendButton setTitle:self.isInvite ? @"邀请" : @"赠送" forState:UIControlStateNormal];
        XMWeakSelf(self);
        cell.sendBlock = ^(XMGiftModel * _Nonnull model) {
            XMStrongSelf(self);
            if (self.sendBlock) {
                self.sendBlock(model, NO, self.isInvite);
            }
        };
        return cell;
    }
    
    if (collectionView == self.memberCollectionV) {
        XMGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMGiftCollectionViewCell) forIndexPath:indexPath];
        cell.model = [self.memberArray safe_objectAtIndex:indexPath.item];
        XMWeakSelf(self);
        cell.sendBlock = ^(XMGiftModel * _Nonnull model) {
            XMStrongSelf(self);
            if (self.sendBlock) {
                self.sendBlock(model, YES, self.isInvite);
            }
        };
        return cell;
    }
    
    if (collectionView == self.bagCollectionV) {
        XMBagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMBagCollectionViewCell) forIndexPath:indexPath];
        cell.model = [self.bagArray safe_objectAtIndex:indexPath.item];
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.giftCollectionV) {
        [self.giftArray enumerateObjectsUsingBlock:^(XMGiftModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == indexPath.row) {
                obj.selected = YES;
            } else {
                obj.selected = NO;
            }
        }];
        [self.giftCollectionV reloadData];
    }
    
    if (collectionView == self.memberCollectionV) {
        [self.memberArray enumerateObjectsUsingBlock:^(XMGiftModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == indexPath.row) {
                obj.selected = YES;
            } else {
                obj.selected = NO;
            }
        }];
        [self.memberCollectionV reloadData];
    }
    
    if (collectionView == self.bagCollectionV) {
        [self.bagArray enumerateObjectsUsingBlock:^(XMBagModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == indexPath.row) {
                obj.selected = YES;
            } else {
                obj.selected = NO;
            }
        }];
        [self.bagCollectionV reloadData];
    }
}

- (IBAction)clickAct:(UIButton *)sender {
    if (self.isInvite) {
        self.giftButton.selected = YES;
        self.memberCollectionV.hidden = YES;
        self.bagCollectionV.hidden = YES;
        self.memberButton.hidden = YES;
        self.bagButton.hidden = YES;
        self.scrollerView.scrollEnabled = NO;
        return;
    }
    self.scrollerView.scrollEnabled = YES;
    switch (sender.tag) {
        case 0: {
            self.giftButton.selected = YES;
            self.memberButton.selected = NO;
            self.bagButton.selected = NO;
            self.giftCollectionV.hidden = NO;
            self.memberCollectionV.hidden = NO;
            [self.scrollerView setContentOffset:CGPointMake(0, 0)];
        } break;
        case 1: {
            self.giftButton.selected = NO;
            self.memberButton.selected = YES;
            self.bagButton.selected = NO;
            self.giftCollectionV.hidden = NO;
            self.memberCollectionV.hidden = NO;
            [self.scrollerView setContentOffset:CGPointMake(kScreenWidth, 0)];

        } break;
        case 2: {
            self.giftButton.selected = NO;
            self.memberButton.selected = NO;
            self.bagButton.selected = YES;
            self.giftCollectionV.hidden = NO;
            self.memberCollectionV.hidden = NO;
            [self.scrollerView setContentOffset:CGPointMake(kScreenWidth * 2, 0)];
        } break;
        default:
            break;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / kScreenWidth;
    switch (index) {
        case 0:
            self.giftButton.selected = YES;
            self.memberButton.selected = NO;
            self.bagButton.selected = NO;
            break;
        case 1:
            self.giftButton.selected = NO;
            self.memberButton.selected = YES;
            self.bagButton.selected = NO;
            break;
        case 2:
            self.giftButton.selected = NO;
            self.memberButton.selected = NO;
            self.bagButton.selected = YES;
            break;
        default:
            break;
    }
}

- (IBAction)recharge:(UIButton *)sender {
    if (self.rechargeBlock) {
        self.rechargeBlock();
    }
}

- (void)reloadGolod {
    [self.moneyButton setTitle:kUserModel.golds forState:UIControlStateNormal];
}

- (XMGiftOpenMemberView *)openMemberView {
    if (!_openMemberView) {
        _openMemberView = [XMGiftOpenMemberView viewFromXib];
    }
    return _openMemberView;
}

@end
